///////////////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2011. Dimarik
//
///////////////////////////////////////////////////////////////////////////////

package
{

import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Graphics;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.EventPhase;
import flash.events.MouseEvent;
import flash.net.URLRequest;

import ru.goodcore.core.BoxTracer;
import ru.goodcore.core.GoodCore;
import ru.goodcore.display.GIFLoaderAdv;
import ru.goodcore.display.ui.core.UIComponent;
import ru.goodcore.logging.Log;
import ru.goodcore.logging.targets.ILoggingTarget;
import ru.goodcore.logging.targets.TraceTarget;

/**
 *  @author				Dimarik
 *  @version			1.0
 *  @langversion		3.0
 *  @playerversion		9.0
 * 
 */
[SWF(width="600", height="400", frameRate="21", backgroundColor="#E0EEEE")]

public class LayoutTest extends GoodCore {

	//-------------------------------------------------------------------------
	//
	//   Constructor
	//
	//-------------------------------------------------------------------------

	public function LayoutTest() {
		var target:ILoggingTarget = new TraceTarget();
		//var target:ILoggingTarget = new DeMonsterDebuggerTarget();
		Log.addTarget(target);

		super();
		super.percentWidth = 100;
		super.percentHeight = 100;
		//super.width = 400;
		//super.height = 400;
		//super.clipContent = true;
		super.color = 0xFF9200;

		var layoutTracer:BoxTracer = this.layoutTracer as BoxTracer;
		layoutTracer.gap = -6;
		layoutTracer.paddingTop = -10;
		layoutTracer.paddingBottom = -10;
		layoutTracer.horizontal = false;
		layoutTracer.verticalAlign = 1;
	}

	//-------------------------------------------------------------------------
	//
	//   Private variables
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//   Private methods
	//
	//-------------------------------------------------------------------------

	protected override function creationComplete():void {
		super.creationComplete();
		super.stage.scaleMode = StageScaleMode.NO_SCALE;
		super.stage.align = StageAlign.TOP_LEFT;

		this.createUISet();
	}

	/**
	 * @private
	 */
	private function createUISet():void {

		var ui:				UIComponent;
		var child:			DisplayObject;
		var parent:DisplayObjectContainer = super.parent;
		var container:		DisplayObjectContainer = this;
		//var container:		DisplayObjectContainer = parent;
		//var container:		DisplayObjectContainer = new UIComponent();
		container.addEventListener(MouseEvent.CLICK, this.handler_containerClick);

		//super.parent.removeChild(this);
		//parent.addChild(this);

		var numObjects:		uint = 0;

		for (var i:uint = 0; i < numObjects; i++) {
			child = this.createSprite();
			//child.width = 100;
			//child.height = 100;
			container.addChild(child);
			//child.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick);
		}

		numObjects = 0;

		for (i = 0; i < numObjects; i++) {
			ui = new UIChildDepent();
			ui.layoutTracer = new BoxTracer();
			//ui.percentWidth = 60;
			//ui.percentHeight = 80;
			//ui.width = 200;
			//ui.height = 100;
			ui.clipContent = true;
			ui.color = 0x00FF00;
			container.addChild(ui);
			ui.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick2);
		}

		//container = ui;

		numObjects = 0;

		for (i = 0; i < numObjects; i++) {
			child = this.createSprite();
			//child.width = 100;
			//child.height = 100;
			container.addChild(child);
			//child.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick);
		}

		
		const gifLoader:GIFLoaderAdv = new GIFLoaderAdv(false, true, super.stage.frameRate);
		//gifLoader.load( new URLRequest('http://megapont.ru/images/work/42_4.gif') );
		gifLoader.load( new URLRequest('http://www.pixeljoint.com/files/icons/full/gordoculiao.png') );
		gifLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, this.handler_loadComplete);
		gifLoader.addEventListener(MouseEvent.CLICK, this.handler_gifLoaderMouseClick);
		container.addChild(gifLoader);
		

		numObjects = 2;
		var layoutTracer:	BoxTracer;

		for (i = 0; i < numObjects; i++) {
			ui = new UIComponent();
			//ui.percentWidth = 50;
			//ui.percentHeight = 50;
			ui.width = 100;
			ui.height = 50;
			ui.color = 0xFF00FF;
			container.addChild(ui);
			ui.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick);
		}

		//container = ui;

		//ui.layoutTracer = new BoxTracer();

		numObjects = 0;

		for (i = 0; i < numObjects; i++) {
			ui = new UIComponent();
			ui.percentWidth = 50;
			ui.percentHeight = 80;
			//ui.width = 100;
			//ui.height = 100;
			ui.color = 0xFFFFFF;
			container.addChild(ui);
			ui.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick);
		}

		container = ui;

		numObjects = 0;

		for (i = 0; i < numObjects; i++) {
			child = this.createSprite();
			//child.width = 100;
			//child.height = 100;
			container.addChild(child);
			child.addEventListener(MouseEvent.CLICK, this.handler_uiMouseClick);
		}

	}

	private function handler_loadComplete(event:Event):void {
		var li:			LoaderInfo = event.target as LoaderInfo;
		var loader:		Loader = li.loader;
		var content:	DisplayObject = loader.content;

		li.removeEventListener(Event.COMPLETE,			this.handler_loadComplete);
		//super.addChild(loader);

		if (loader.parent is UIComponent)
			(loader.parent as UIComponent).invalidate();
	}

	/**
	 * @private
	 */
	private function createSprite():DisplayObject {
		const s:Sprite = new Sprite();
		const g:Graphics = s.graphics;
		g.beginFill(0x00FFFF, 0.3);
		g.drawRect(0, 0, 50, 50);
		g.endFill();
		return s;
	}

	//-------------------------------------------------------------------------
	//
	//   Events handlers
	//
	//-------------------------------------------------------------------------

	private function handler_uiMouseClick(event:MouseEvent):void {
		if (event.eventPhase === EventPhase.AT_TARGET ) {
			//return;
		}

		const uiComponent:UIComponent = event.target as UIComponent;

		//uiComponent.percentWidth = 10 + int(Math.random() * 90);
		//uiComponent.percentHeight = 10 + int(Math.random() * 90);

		if ( uiComponent.percentWidth ) {
			uiComponent.width = int(Math.random() * 300) + 200;
		} else {
			//uiComponent.percentWidth = 10 + int(Math.random() * 90);
			uiComponent.percentWidth = 100;
		}

	
		if ( uiComponent.percentHeight ) {
			uiComponent.height = int(Math.random() * 300) + 200;
		} else {
			//uiComponent.percentHeight = 10 + int(Math.random() * 90);
			uiComponent.percentHeight = 100;
		}
	

		//uiComponent.height = int(Math.random() * 300) + 100;
		//uiComponent.width = 320;

		event.stopImmediatePropagation();

		//uiComponent.width = int(Math.random() * 300) + 100;
		//uiComponent.width = 320;
	}

	private function handler_uiMouseClick2(event:MouseEvent):void {
		if (event.eventPhase === EventPhase.AT_TARGET ) {
			//return;
		}

		const uiComponent:UIComponent = event.target as UIComponent;
		
		if (!uiComponent)
			return;

		//uiComponent.percentWidth = 10 + int(Math.random() * 90);
		//uiComponent.percentHeight = 10 + int(Math.random() * 90);

		if ( uiComponent.percentWidth ) {
			uiComponent.width = int(Math.random() * 300) + 100;
		} else {
			//uiComponent.percentWidth = 10 + int(Math.random() * 90);
			uiComponent.percentWidth = 100;
		}

		if ( uiComponent.percentHeight ) {
			uiComponent.height = int(Math.random() * 300) + 100;
		} else {
			//uiComponent.percentHeight = 10 + int(Math.random() * 90);
			uiComponent.percentHeight = 100;
		}


		//uiComponent.height = int(Math.random() * 300) + 100;
		//uiComponent.width = 320;

		event.stopImmediatePropagation();

		//uiComponent.width = int(Math.random() * 300) + 100;
		//uiComponent.width = 320;
	}

	private function handler_gifLoaderMouseClick(event:MouseEvent):void {
		const gifLoader:Loader = event.target as Loader;
		gifLoader.load( new URLRequest('http://megapont.ru/images/work/42_4.gif') );
		//gifLoader.load( new URLRequest('http://www.pixeljoint.com/files/icons/nauseating_headache_bunee.gif') );
		event.stopImmediatePropagation();
	}

	private function handler_containerClick(event:MouseEvent):void {
		const uiComponent:UIComponent = event.currentTarget as UIComponent;
		if (event.eventPhase === EventPhase.AT_TARGET ) {
			uiComponent.percentWidth = 100;
		} else {
			if ( isNaN(uiComponent.percentWidth) ) {
				uiComponent.percentWidth = 100;
			} else {
				uiComponent.width = 400;
			}
		}
	}

	private function handler_parentDepentUICMouseClick(event:MouseEvent):void {
		if (event.eventPhase === EventPhase.AT_TARGET ) {
			const uiComponent:UIComponent = event.target as UIComponent;
			uiComponent.percentWidth = 50;
		}
	}

}
}
import ru.goodcore.display.ui.core.UIComponent;

internal class UIChildDepent extends UIComponent {

	public function UIChildDepent() {
		super();
	}
}